package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.Comment;
import com.gdit.zax.entitys.VideoALL;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;
import com.gdit.zax.utils.webUtil;

public class GetClassDetails extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int cid = TextUtil.String2int(request.getParameter("id"), -1);
		
		
		//检测参数
		if (cid < 0) {
			O.printFail(response, 1, "请检查id参数!");
			return;
		}
		
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		VideoDao vdao = dao.getVideoDao();
		ArrayList<VideoALL> list = vdao.listAllVideobycourse(cid,1,10000);//该课程所有信息
		if (list.size() == 0) {
			O.printFail(response, 1, "该课程没有视频!");
			return;
		}
		
		
		Map<Integer, ArrayList<VideoALL>> sid2vid = new HashMap<Integer, ArrayList<VideoALL>>();
		for (VideoALL videoALL : list) {
			ArrayList<VideoALL> sublist = sid2vid.get(videoALL.getT_section_id());
			if (sublist == null) sublist = new ArrayList<VideoALL>();
			sublist.add(videoALL);
			sid2vid.put(videoALL.getT_section_id(), sublist);
		}
		
		ArrayList<Map<String, Object>> sections = new ArrayList<Map<String,Object>>();
		for (Integer key : sid2vid.keySet()) {
			Map<String, Object> submap = new HashMap<String, Object>();
			ArrayList<VideoALL> subvideos = sid2vid.get(key);
			submap.put("id", key);
			if (subvideos.size()>0) {
				submap.put("name", subvideos.get(0).getT_section_name());				
			}
			
			ArrayList<Map<String, Object>> videos = new ArrayList<Map<String,Object>>();
			for (int i = 0; i < subvideos.size(); i++) {
				VideoALL video = subvideos.get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", video.getT_video_id()); 
				map.put("name", video.getT_video_name()); 
				map.put("sectionid", video.getT_section_id()); 
				map.put("url", Constant.WEBRoot + video.getT_video_url() + "?type=1&accesstoken=" + accesstoken); 
				map.put("comment", new ArrayList<Comment>()); 
				map.put("downurl",Constant.WEBRoot + video.getT_video_url() + "?type=2&accesstoken=" + accesstoken); 
				videos.add(map);
			}
			
			submap.put("videos", videos);
			sections.add(submap);
		}
		
		Map<String, Object> outmap = new HashMap<String, Object>();
		if (list.size()>0) {
			
			
//			coursename	string	课程名称
//			Introduction		课程简介
//			seesum	int	观看人数
//			collectnum	Int	收藏人数
//			commentsum	int	留言总数
			outmap.put("userintroduction", list.get(0).getT_user_introduction());
			outmap.put("coursename", list.get(0).getT_course_name());
			outmap.put("courseintroduction", list.get(0).getT_course_introduction());
			outmap.put("seesum", list.get(0).getT_course_seesum());
			outmap.put("collectnum", list.get(0).getT_course_ccollectsum());
			outmap.put("commentsum", list.get(0).getT_course_commentsum());
			outmap.put("head", webUtil.getWebAbsoluteUrl(list.get(0).getT_user_userhead(), Constant.PIC_userhead));
			outmap.put("teacherid", list.get(0).getT_user_id());
			
			outmap.put("tlevel", list.get(0).getT_user_tlevel());
			outmap.put("Content", list.get(0).getT_user_nick());
			outmap.put("videointroduction", list.get(0).getT_video_introduction());
		}
		outmap.put("sections", sections);
		
		
		O.printSuccess(response, outmap);
		
		
		
	}

}
